package com.shine.app.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.adapter.FlipperGallery;
import com.shine.app.bean.CourseImageBean;
import com.shine.app.bean.GolfCourseDetailBean;
import com.shine.app.bean.MyReviewBean;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfoReview_detailActivity extends BaseActivity implements
		OnClickListener {

	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;
	@InjectView(R.id.head_number)
	private TextView head_numberTextView;
	@InjectView(R.id.head_save)
	private TextView head_saveTextView;

	protected FlipperGallery gallery;
	@InjectView(R.id.gallery)
	protected ViewFlipper viewFlipper;

	@InjectView(R.id.progressBar)
	protected ProgressBar progressBar;
	@InjectView(R.id.layout)
	protected LinearLayout layout;

	@InjectView(R.id.ratingbar)
	private RatingBar ratingBar;
	@InjectView(R.id.name)
	private TextView nameTextView;
	@InjectView(R.id.content)
	private TextView contentTextView;
	@InjectView(R.id.id)
	private TextView idTextView;
	@InjectView(R.id.edit)
	private Button editButton;
	@InjectView(R.id.delete)
	private Button deletebButton;
	private MyReviewBean myReviewBean;
	private String golferId;
	private int courseId;
	private String number;
	private CourseReviewDetailAsyncTask courseReviewDetailAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewmyinfo_reviews_detail);
		Injector.get(this).inject();
		init();
		courseReviewDetailAsyncTask = new CourseReviewDetailAsyncTask();
		courseReviewDetailAsyncTask.execute(golferId, courseId);
	}

	private void init() {
		number = getIntent().getStringExtra("number");
		myReviewBean = (MyReviewBean) getIntent().getSerializableExtra("bean");
		courseId = myReviewBean.getCourseId();
		golferId = appContext.getLoginInfo().getUser().getGolferId();

		head_nameTextView.setText(R.string.view_reviews);
		head_numberTextView.setText("(" + number + ")");
		head_backImageView.setOnClickListener(this);
		editButton.setOnClickListener(this);
		deletebButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("myReviewBean", myReviewBean);
		switch (v.getId()) {

		case R.id.head_back:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		case R.id.edit:
			editButton.startAnimation(buttonAnimations);
			intent.setClass(this, ViewMyInfoReview_editActivity.class);
			this.startActivity(intent);
			overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
			break;

		case R.id.delete:
			deletebButton.startAnimation(buttonAnimations);
			intent.setClass(this, ViewMyInfo_ReviewDeleteDialogActivity.class);
			this.startActivity(intent);
			break;

		default:
			break;
		}
	}

	public class CourseReviewDetailAsyncTask extends
			AsyncTask<Object, Integer, GolfCourseDetailBean> {
		private int courseId;
		private String golferId;

		@Override
		protected GolfCourseDetailBean doInBackground(Object... params) {
			GolfCourseDetailBean bean = null;
			golferId = (String) params[0];
			courseId = (Integer) params[1];
			bean = appContext.getMyCourseDetail(golferId, courseId);
			if (bean == null) {
				bean = new GolfCourseDetailBean();
			}
			return bean;
		}

		@Override
		protected void onPostExecute(GolfCourseDetailBean result) {
			super.onPostExecute(result);
			progressBar.setVisibility(View.GONE);
			layout.setVisibility(View.VISIBLE);
			iniView(result);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
			layout.setVisibility(View.GONE);
		}
	}

	private void iniView(GolfCourseDetailBean golfCourseDetailBean) {
		ratingBar.setRating((float) golfCourseDetailBean.getRating());
		nameTextView.setText(golfCourseDetailBean.getName());
		contentTextView.setText(golfCourseDetailBean.getDescription());

		CourseImageBean[] images = golfCourseDetailBean.getImages();
		if (images != null && images.length > 0) {
			gallery = new FlipperGallery(this, viewFlipper, images, imageLoader);
		} else {
			images = new CourseImageBean[1];
			images[0] = new CourseImageBean("", "");
			gallery = new FlipperGallery(this, viewFlipper, images, imageLoader);
		}
	}

	@Override
	protected void onDestroy() {
		if (courseReviewDetailAsyncTask != null
				&& courseReviewDetailAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			courseReviewDetailAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
